from rest_framework.filters import BaseFilterBackend


class MyFilter(BaseFilterBackend):
    def filter_queryset(self, request, queryset, view):
        min_price = request.query_params.get('min_price')
        max_price = request.query_params.get('max_price')
        if min_price and max_price:
            queryset = queryset.filter(price__gte=min_price,price__lte=max_price)
        return queryset


from django_filters.filters import Filter
from . import models
from django_filters import filters
class CourseFilterSet(Filter):
    # 区间过滤：field_name关联的model字段
    # lookup_expr设置规则，gt是大于，gte是大于等于
    min_price = filters.NumberFilter(field_name='price',lookup_expr='gte')
    max_price = filters.NumberFilter(field_name='price',lookup_expr='lte')
    class Meta:
        model = models.Course
        fields = ['course_category', 'min_price', 'max_price']
